// pages/agree_refund/index.js
const app = getApp()
const Host = require('../../config').Host
Page({

    /**
     * 页面的初始数据
     */
    data: {
        total_money:120,//应退款总额
        refund_money:100,//实际退回的钱
        state:0,
        isTanShow:false,
        inputValue1:'',
        inputValue2: '',
        inputValue3: '',
        sellerPhone:'',
        getPeopleMessage:{},
        refund:{},
        orderid:0,
        express_name:'',
        express:'',
    },
    //打开弹窗
    tanShow:function(){
        this.setData({
            isTanShow:true,
        })
    },
    //关闭弹窗
    close:function(){
        this.setData({
            isTanShow:false,
        })
    },
    getInputValue1:function(e){
        this.setData({
            inputValue1:e.detail.value,
        })
    },
    getInputValue2: function (e) {
        this.setData({
            inputValue2: e.detail.value,
        })
    },
    getInputValue3: function (e) {
        this.setData({
            inputValue3: e.detail.value,
        })
    },
    //提交
    sure:function(){
        var that=this;
        if (!that.data.express_name){
            wx.showToast({
                title: '请选择物流公司',
                icon:'none',
            })
        }else if(!that.data.inputValue2){
            wx.showToast({
                title: '请填写物流单号',
                icon: 'none',
            })
        }
        // else if (!that.data.inputValue3) {
        //     wx.showToast({
        //         title: '请填写手机号码',
        //         icon: 'none',
        //     })
        // }
        else{
          var orderid = that.data.orderid
          var expresscom = that.data.express_name
          var express = that.data.express
          var expresssn = that.data.inputValue2

          wx.request({
            url: Host + 'Mine/saveexpress',
            data: { orderid, expresscom, express, expresssn},
            method: 'post',
            header: { 'content-type': 'application/x-www-form-urlencoded' },
            success: function (res) {
              //console.log(res.data);
              if (res.data.code == 200) {
                that.setData({
                  isTanShow: false,
                  state: 1,
                })
                wx.showToast({
                  title: '提交成功',
                })
                setTimeout(function () {
                  wx.navigateTo({
                    url: '/pages/refund/refund',
                  })
                }, 1000)
              } else {
                wx.showToast({
                  title: res.data.errmsg,
                  icon: 'none',
                })
              }
            }
          })
        }
    },
    call:function(){
        var that=this;
        wx.makePhoneCall({
            phoneNumber:that.data.sellerPhone,
        })
    },
  //撤销申请
  cancelapply: function (e) {
    var that = this;
    var orderid = that.data.orderid;

    wx.showModal({
      title: '',
      content: '确认撤销退款申请么',
      success: function (res) {
        if (res.cancel) {
          //console.log("暂时不取消")
        } else {
          wx.request({
            url: Host + 'Mine/cancelapply',
            data: { orderid},
            success: function (res) {
              //console.log(res.data);
              wx.showToast({
                title: '撤销成功',
                icon: 'none',
              })
              if (res.data.code == 200) {
                setTimeout(function () {
                  wx.navigateTo({
                    url: '/pages/refund/refund',
                  })
                }, 1000)
              } else {
                wx.showToast({
                  title: res.data.errmsg,
                  icon: 'none',
                })
              }
            }
          })
        }
      }
    })
  },
  bindPickerChange: function (e) {
    //console.log(e.detail.value)
    var that = this;
    var express_list = that.data.express_list;
    var express_name = express_list[e.detail.value].name;
    var express = express_list[e.detail.value].express;
    //console.log(id)
    that.setData({
      express_name: express_name,
      express: express,
    })
  },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
       var that=this;
       that.setData({
         orderid: options.id
       })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      var that=this;
      var orderid = that.data.orderid;

      wx.request({
        url: Host + 'Mine/drawbackDetail',
        data: { orderid },
        success: function (res) {
          //console.log(res.data);
          if (res.data.code == 200) {
            that.setData({
              refund: res.data.refund,
              sellerPhone: res.data.refund.telephone,
              state: res.data.state,
              getPeopleMessage: res.data.getPeopleMessage,
              express_list: res.data.express_list,
              success_refund_time: res.data.refund.success_refund_time
            })
          }
        }
      })

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})